package interview.huawei.myself.online1.v2;

import java.util.concurrent.TimeUnit;

/**
 * @Author CandyWall
 * @Date 2022/3/13--11:32
 * @Description
 */
public class Consumer extends Thread{
    private String name;
    private MessageQueue messageQueue;
    private int type;

    public Consumer(String name, MessageQueue messageQueue, int type) {
        super(name);
        this.name = name;
        this.messageQueue = messageQueue;
        this.type = type;
        start();
    }

    @Override
    public void run() {
        while (true) {
            int num;
            if (type == 0) {
                num = messageQueue.getEvenNumber();
            } else {
                num = messageQueue.getOddNumber();
            }
            System.out.println(name + " 消费了 " + num);
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
